package cn.cqie.frame.config;


import cn.cqie.frame.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器配置
 * @author xieyihong
 */
@Configuration
public class InterceptConfig extends WebMvcConfigurationSupport {

    private TokenInterceptor tokenInterceptor;

    //构造方法
    public InterceptConfig(TokenInterceptor tokenInterceptor) {
        this.tokenInterceptor = tokenInterceptor;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //登录
        excludePath.add("/login");
//        excludePath.add("/logout"); //登出
        //静态资源
        excludePath.add("/static/**");
        //静态资源
        excludePath.add("/assets/**");
        // swagger过滤
        excludePath.add("/favicon.ico");
        excludePath.add("/actuator/health/**");
        excludePath.add("/v2/api-docs/**");
        excludePath.add("/v2/api-docs-ext/**");
        excludePath.add("/swagger-resources");
        excludePath.add("/error/**");
        excludePath.add("/webjars/**");
        excludePath.add("/swagger-ui.html");
        excludePath.add("/doc.html");

        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        super.addInterceptors(registry);
    }

}